package com.ssm.helong.shop.controller;

import com.github.pagehelper.PageInfo;
import com.ssm.helong.shop.entity.Address;
import com.ssm.helong.shop.service.AddressService;
import com.ssm.helong.shop.util.Result;
import com.ssm.helong.shop.util.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@Controller
@ResponseBody
@RequestMapping("/Address")
public class AddressController {
    @Autowired
    private AddressService addressService;

    /***
     * 查询用户地址
     * @param userId
     * @param pageNo
     * @param pageSize
     * @return: com.ssm.helong.shop.util.Result<com.ssm.helong.shop.entity.Address>
     * @Author: helong.sun
     * @Date: 2021/5/1
     */
    @RequestMapping(value = "/selectAddress", method = RequestMethod.POST)
    public Result <Address> selectAddress(@RequestParam(value = "userId") Integer userId,
                                          @RequestParam(value = "pageNo") int pageNo,
                                          @RequestParam(value = "pageSize") int pageSize) {
        PageInfo <Address> addressList = addressService.selectAddress(userId, pageNo, pageSize);
        return ResultGenerator.genSuccessResult(addressList);
    }

    /***
     * 修改用户地址
     * @param address
     * @return: java.lang.String
     * @Author: helong.sun
     * @Date: 2021/5/1
     */
    @RequestMapping(value = "/updateAddress", method = RequestMethod.POST)
    @ResponseBody
    public Result updateAddress(@RequestBody Address address) {
        if (addressService.updateAddress(address)) {
            return ResultGenerator.genSuccessResult("修改成功");
        }
        return ResultGenerator.genFailResult("修改失败");
    }

    /***
     * 添加用户地址
     * @param address
     * @return: com.ssm.helong.shop.util.Result
     * @Author: helong.sun
     * @Date: 2021/5/2
     */
    @RequestMapping(value = "/addAddress")
    public Result addAddress(Address address) {
        if (addressService.addAddress(address)) {
            return ResultGenerator.genSuccessResult("添加成功");
        }
        return ResultGenerator.genFailResult("添加失败");
    }

    /***
     * 删除用户地址
     * @param id
     * @return: com.ssm.helong.shop.util.Result
     * @Author: helong.sun
     * @Date: 2021/5/2
     */
    @RequestMapping("deleteAddress")
    public Result deleteAddress(Integer id) {
        if (addressService.deleteAddress(id)) {
            return ResultGenerator.genSuccessResult("删除成功");
        }
        return ResultGenerator.genFailResult("删除失败");
    }

    /**
     * 查询一个地址信息
     * @param id
     * @return: com.ssm.helong.shop.util.Result<com.ssm.helong.shop.entity.Address>
     * @Author: helong.sun
     * @Date: 2021/5/2
     */
    @RequestMapping("selectOneAddress")
    public Result <Address> selectOneAddress(Integer id) {
        Address address = addressService.selectOneAddress(id);
        return ResultGenerator.genSuccessResult(address);
    }

}
